<template>
  <div class="container">
    <el-card class="box-card" style="width: 100%">
      <!-- 头部 -->
      <div slot="header" class="clearfix">
        <div class="left" style="float: left">
          <span class="title-name">标签名称</span>
          <el-input
          class="input"
            v-model="page.tagName"
            placeholder="请输入内容"
            style="width: 190px"
          />
          <span class="title-name">状态</span>
          <el-select v-model="page.state" placeholder="请选择" class="select">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-button @click="clearFn">清除</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </div>
        <el-button
          type="success"
          style="float: right"
          icon="el-icon-edit"
          @click="addSubjects"
          >新增目录</el-button
        >
      </div>
      <!-- 提示 -->
      <el-row class="info"
        ><i class="el-icon-info" /> 数据一共 {{ counts }} 条</el-row
      >
      <!-- 表格 -->
      <div class="form">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="80" />
          <el-table-column prop="subjectName" label="所属学科" width="164" />
          <el-table-column prop="tagName" label="标签名称" width="161" />
          <el-table-column prop="username" label="创建者" width="161" />
          <el-table-column prop="addDate" label="创建日期" width="161" />
          <el-table-column prop="state" label="状态" width="150" />
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button type="text" @click="changeFn(row.id)">{{
                isOpen(row.state)
              }}</el-button>
              <el-button type="text" :disabled="disableds">修改</el-button>
              <el-button
                type="text"
                :disabled="disableds"
                @click="delFn(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="Pagination"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="page.pagesize"
          :total="counts"
          layout="prev, pager, next ,sizes, jumper"
        >
        </el-pagination>
      </div>
    </el-card>
    <TagsAdd :dialogVisible.sync="dialogVisible" @reflesh="getTagsList()" />
  </div>
</template>

<script>
import TagsAdd from '@/module-hmmm/components/tags-add.vue'
import { list, remove } from '@/api/hmmm/tags.js'
export default {
  data () {
    return {
      tableData: [],
      counts: 0,
      page: {
        page: 1,
        pagesize: 10,
        tagName: '',
        state: ''
      },
      options: [
        {
          value: '1',
          label: '开启'
        },
        {
          value: '0',
          label: '屏蔽'
        }
      ],
      disableds: false,
      dialogVisible: false
    }
  },
  components: {
    TagsAdd
  },
  created () {
    this.getTagsList()
  },
  methods: {
    isOpen (a) {
      // console.log(a)
      if (a === '开启') return '开启'
      else return '屏蔽'
    },
    async getTagsList () {
      const res = await list(this.page)
      // console.log(res)
      res.data.items.forEach((item) => {
        item.addDate = item.addDate.replace('T', ' ')
      })
      res.data.items.forEach((item) => {
        item.addDate = item.addDate.replace('.000Z', ' ')
      })
      res.data.items.forEach((item) => {
        item.state = item.state === 1 ? '开启' : '屏蔽'
      })
      this.tableData = res.data.items
      // console.log(res)
      this.counts = res.data.counts
    },
    addSubjects () {
      this.dialogVisible = true
    },
    async delFn (id) {
      await remove({ id })
      this.$message.success('删除成功')
      this.getTagsList()
    },
    changeFn () {
      this.disableds = !this.disableds
      this.isOpen()
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.page.pagesize = val
      this.getTagsList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.page.page = val
      this.getTagsList()
    },
    clearFn () {
      this.page.tagName = ''
      this.page.state = ''
      this.getTagsList()
    },
    search () {
      this.getTagsList()
    }
  }
}
</script>

<style lang="less" scoped>
.text {
  font-size: 14px;
}
.item {
  margin-bottom: 18px;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
.box-card {
  width: 480px;
}
.left .title-name {
  font-size: 14px;
  color: #606266;
  font-weight: 700;
}
.left .el-input {
  margin-left: 10px;
}
.left .el-button {
  margin-left: 10px;
}
.info {
  height: 35px;
  line-height: 35px;
  background-color: #f4f4f5;
  color: #909399;
  padding-left: 10px;
}
.form {
  margin-top: 20px;
}
.el-table {
  ::v-deep .el-table__header-wrapper {
    th {
      background-color: #fafafa;
    }
  }
}
.Pagination{
  float: right;
  margin: 20px 0px;
}
.input{
  margin-right: 40px;
}
.select{
  margin-left: 10px;
}
</style>
